package com.hrt.test.main;

import com.btcode.common.BitType;
import com.btcode.common.ByteUnit;
import com.btcode.db.result.Row;
import com.hrt.datasender.datadao.service.BdSourceService;
import com.hrt.datasender.datastorage.strategy.StrategyLoader;
import com.hrt.datasender.process.strategy.hj212.MessagePraser;
import com.hrt.datasender.process.strategy.hj212.util.CRCUtil;
import com.hrt.datasender.process.strategy.hj212.util.DataType;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.*;

/**
 * Created by Chance on 2017/11/14.
 */
public class AppStart {
    static ProcessBuilder pb = new ProcessBuilder("java", "Test");
    static Process p;
    private static final char HexCharArr[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            /**
             * C1 48 00 00  = 12.5
             * 55 A1 F0 40
             * 正常解析应该是F04055A1解析即可
             * 中兴的解析变成A15540F0
             * F7 9B 95 3E
             *
             *
             * 4D 01 40 24
             */
            String dataItem = "3BB8F4C6";
            dataItem = "4D014024";
            dataItem = "40240000";
            byte[] sInput = new byte[0];
            sInput = hexStringToByteArray(dataItem);

            String test = byteArrToHex(sInput);
            double value = ByteUnit.ConvertByteToDoubleX(sInput, BitType.bit32);
            System.out.println(value);


//            //Original byte[]
//            byte[] bytes = "hello world".getBytes();
//
//            //Base64 Encoded
//            String encoded = new String(bytes);
//            System.out.println(encoded);

//            String msg = "Z0,12.356,2020-12-07 10: 0:12,140,00000000000000000000000000000000<ZEEI>\n";
//            System.out.println(msg+"结束");
//            msg = msg.substring(msg.length()-7);
//            System.out.println(msg);
//            if(msg.startsWith("Z0")&&msg.endsWith("<ZEEI>")){
//                System.out.println(msg);
//            }
//            String test = "<ZEEI>";
//            System.out.println(test);
//            System.out.println(test.toUpperCase());
//            if("ZEEI".equals(test)){
//                System.out.println("111");
//            }


//            Double pHAvg = null;
//            // 氢离子
//            Double hydrion = 0.0;
//            // 氢氧根离子
//            Double hydrionIon = 0.0;
//            //pH计算底数
//            final int phBaseNumber = 10;
//            //pH最大值
//            final int phMaxValue = 14;
//            //pH中间值
//            final int phInterValue = 7;
//
//
//
//            List<Row> rows = new ArrayList<Row>();
//            Row row1 = new Row();
//            row1.setValue("MEAS_RE",8.13);
//            Row row2 = new Row();
//            row2.setValue("MEAS_RE",8.13);
//            rows.add(row1);
//            rows.add(row2);
//
//
//            if (rows != null && rows.size() > 0) {
//                for (Row row : rows) {
//                    // 取出每个pH值
//                    Double pH = Double.valueOf(row.getSafeString("MEAS_RE"));
//                    hydrion += Math.pow(phBaseNumber, -pH);
//                    hydrionIon += Math.pow(phBaseNumber, -(phMaxValue - pH));
//                }
//                // 判断混合后是酸性还是碱性
//                if (hydrion >= Math.pow(phBaseNumber, -phInterValue)) {
//                    // 酸性
//                    pHAvg = -log10(hydrion / rows.size(), phBaseNumber);
//                }
//                else {
//                    // 碱性
//                    pHAvg = -log10(hydrionIon / rows.size(), phBaseNumber);
//                }
//            }
//            System.out.println(pHAvg);


//            final int dataLengthFormat = 4;
//            StringBuilder strBuilder = new StringBuilder();
//            strBuilder.append("QN=20190225171331574;ST=32;CN=2061;PW=123456;MN=WWSZ0003070053;Flag=3;CP=&&BeginTime=20190225010000,EndTime=20190225020000");
//
//            String crcStr = CRCUtil.calcCRC(strBuilder.toString());
//
//            String dataLength = strBuilder.length() + "";
//            while (dataLength.length() < dataLengthFormat) {
//                dataLength = "0" + dataLength;
//            }
//
//            strBuilder.insert(0, "##" + dataLength);
//
//            strBuilder.append(crcStr + "\r\n");
//            System.out.println(strBuilder.toString());



//            StringBuffer sb = new StringBuffer("##0246QN=20190116154010000;ST=32;CN=2051;PW=123456;MN=399HOLL0000396;CP=&&DataTime=20190116154000;" +
//                    "011-Avg=92.792,011-Flag=N;011-Avg=7.384632629040243,011-Flag=N;B01-Avg=8.188,B01-Cou=5.0," +
//                    "B01-Flag=N;,B01TOTAL-Flag=N;06-Avg=4.143555555555555,06-Flag=N&&;7BC0");
//
//
//            System.out.println("MAC："+getMACAddress());
        } 
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }


    /**
     * 16进制表示的字符串转换为字节数组
     *
     * @param s 16进制表示的字符串
     * @return byte[] 字节数组
     */
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] b = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
                .digit(s.charAt(i + 1), 16));
        }
        return b;
    }

    public static String byteArrToHex(byte[] btArr) {
        char strArr[] = new char[btArr.length * 2];
        int i = 0;
        for (byte bt : btArr) {
            strArr[i++] = HexCharArr[bt>>>4 & 0xf];
            strArr[i++] = HexCharArr[bt & 0xf];
        }
        return new String(strArr);
    }

        // 求底数为10的对数值
    public static double log10(double value, double base) {
        return Math.log(value) / Math.log(base);
    }
    
    //获取MAC地址的方法  
    private static String getMACAddress()throws Exception{  
        final int maskCode = 0xFF;
        InetAddress ia= InetAddress.getLocalHost(); 
        //获得网络接口对象（即网卡），并得到mac地址，mac地址存在于一个byte数组中。  
        byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();   
        //下面代码是把mac地址拼装成String  
        StringBuffer sb = new StringBuffer();  
        for(int i=0;i<mac.length;i++){  
            if(i!=0){  
                sb.append("-");  
            }  
            //mac[i] & 0xFF 是为了把byte转化为正整数  
            String s = Integer.toHexString(mac[i] & maskCode);  
            sb.append(s.length()==1?0+s:s);  
        }  
        //把字符串所有小写字母改为大写成为正规的mac地址并返回  
        return sb.toString().toUpperCase();  
    }


    public static void startBackProcess() {
        try {
            p = pb.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void stopBackProcess() {
        if (p != null && p.isAlive()) {
            p.destroy();
        }
    }

    public static void restartBackProcess() {
        stopBackProcess();
        startBackProcess();
    }

}
